﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Globalization;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using StableDiffusionTools.Enums;

namespace StableDiffusionTools.Controls
{
    public class ImageControlContext : ContextMenuStrip
    {
        //Dictionary<ImageSizeMode, ToolStripMenuItem> imageSizeModeItems = new Dictionary<ImageSizeMode, ToolStripMenuItem>();
        Dictionary<ImageInterpolationMode, ToolStripMenuItem> imageInterpolationModeItems = new Dictionary<ImageInterpolationMode, ToolStripMenuItem>();
        ToolStripMenuItem sizeModeToolStripItem;
        ToolStripMenuItem interpolationModeToolStripItem;
        ToolStripMenuItem playPauseToolStripItem;
        ToolStripMenuItem autoPlayToolStripItem;
        ToolStripMenuItem fpsToolStripItem;

        Control lastSource;

        public ImageControlContext() : base() { Initialize(); }
        public ImageControlContext(IContainer container) : base(container) { Initialize(); }

        private void Initialize()
        {
            //ShowCheckMargin = false;
            //ShowImageMargin = false;
            //Renderer = new MyRenderer();
            playPauseToolStripItem = new ToolStripMenuItem() { Text = "Play/Pause" };
            playPauseToolStripItem.Click += PlayPauseClickedHandler;
            Items.Add(playPauseToolStripItem);

            autoPlayToolStripItem = new ToolStripMenuItem() { Text = "AutoPlay" };
            autoPlayToolStripItem.Click += AutoPlayClickedHandler;
            Items.Add(autoPlayToolStripItem);

            fpsToolStripItem = new ToolStripMenuItem() { Text = "FPS" };
            string[] fpsItems = new string[] { "Auto", "1", "5", "10", "15", "20", "25", "30" };
            for (int i = 0; i < fpsItems.Length; i++)
            {
                ToolStripMenuItem fpsItem = (ToolStripMenuItem)fpsToolStripItem.DropDownItems.Add(fpsItems[i], null, AutoFPSClickedHandler);
            }
            Items.Add(fpsToolStripItem);

            sizeModeToolStripItem = new ToolStripMenuItem() { Text = "Fit Size" };
            sizeModeToolStripItem.Click += SizeModeClickedHandler;
            Items.Add(sizeModeToolStripItem);

            interpolationModeToolStripItem = new ToolStripMenuItem() { Text = "Interpolation" };
            ImageInterpolationMode[] interpolationModes = EnumUtil.GetValues<ImageInterpolationMode>();
            for (int i = 0; i < interpolationModes.Length; i++)
            {
                string interpolationModeName = EnumUtil.GetName(interpolationModes[i]);
                ToolStripMenuItem interpolationModeItem = (ToolStripMenuItem)interpolationModeToolStripItem.DropDownItems.Add(interpolationModeName);
                interpolationModeItem.Click += InterpolationModeClickedHandler;
                imageInterpolationModeItems.Add(interpolationModes[i], interpolationModeItem);
            }
            if (interpolationModeToolStripItem.DropDown is ToolStripDropDownMenu dropDownMenu)
            {
                //dropDownMenu.ShowCheckMargin = false;
                //dropDownMenu.ShowImageMargin = false;
            }
            Items.Add(interpolationModeToolStripItem);
        }

        protected override void OnOpening(CancelEventArgs e)
        {
            base.OnOpening(e);
            lastSource = SourceControl;
            if (lastSource is ImageControl iControl)
            {
                if (iControl.IsAnimation)
                {
                    playPauseToolStripItem.Text = iControl.IsAnimating ? "Pause" : "Play";
                    autoPlayToolStripItem.Checked = iControl.AutoPlay;
                }
                playPauseToolStripItem.Visible = iControl.IsAnimation;
                autoPlayToolStripItem.Visible = iControl.IsAnimation;
                fpsToolStripItem.Visible = iControl.IsAnimation && iControl.IsAnimating;

                ImageSizeMode selectedSizeMode = iControl.SizeMode;
                sizeModeToolStripItem.Checked = selectedSizeMode == ImageSizeMode.Fit;
                ImageInterpolationMode selectedInterpolationeMode = iControl.InterpolationMode;
                ImageInterpolationMode[] allInterpolationModes = EnumUtil.GetValues<ImageInterpolationMode>();
                for (int i = 0; i < allInterpolationModes.Length; i++)
                {
                    ToolStripMenuItem interpolationModeItem = imageInterpolationModeItems[allInterpolationModes[i]];
                    bool check = selectedInterpolationeMode == allInterpolationModes[i];
                    interpolationModeItem.Checked = check;
                    if (check)
                        interpolationModeToolStripItem.Text = $"Interpolation: {EnumUtil.GetName(selectedInterpolationeMode)}";
                }
            }
        }

        private void PlayPauseClickedHandler(object s, EventArgs e)
        {
            if (lastSource is ImageControl iControl && iControl.IsAnimation)
                iControl.IsAnimating = !iControl.IsAnimating;
        }

        private void AutoPlayClickedHandler(object s, EventArgs e)
        {
            if (lastSource is ImageControl iControl && iControl.IsAnimation)
                iControl.AutoPlay = !iControl.AutoPlay;
        }

        private void AutoFPSClickedHandler(object s, EventArgs e)
        {
            if (lastSource is ImageControl iControl && iControl.IsAnimation)
            {
                if(int.TryParse(((ToolStripMenuItem)s).Text, out int fps))
                {
                    iControl.AnimationFPS = fps;
                }
            }
        }

        private void SizeModeClickedHandler(object s, EventArgs e)
        {
            if (lastSource is ImageControl iControl)
            {
                bool sizeFit = sizeModeToolStripItem.Checked = !sizeModeToolStripItem.Checked;
                iControl.SizeMode = sizeFit ? ImageSizeMode.Fit : ImageSizeMode.Zoom;
            }
        }

        private void InterpolationModeClickedHandler(object s, EventArgs e)
        {
            if (lastSource is ImageControl iControl)
                iControl.InterpolationMode = EnumUtil.Parse<ImageInterpolationMode>(((ToolStripMenuItem)s).Text);
        }
    }

    //public class MyRenderer : ToolStripProfessionalRenderer
    //{
    //    private Brush CheckedBrush = new SolidBrush(Color.FromArgb(15, Color.Black));
    //    protected override void OnRenderItemText(ToolStripItemTextRenderEventArgs e)
    //    {
    //        base.OnRenderItemText(e);
    //        if(e.Item is ToolStripMenuItem menuItem && menuItem.Checked)
    //        {
    //            Rectangle rc = new Rectangle(new Point(2, 0), new Size(e.Item.Size.Width - 4, e.Item.Size.Height - 1));
    //            e.Graphics.FillRectangle(CheckedBrush, rc);
    //            //e.Graphics.DrawRectangle(Pens.Blue, 2, 0, rc.Width - 4, rc.Height - 1);
    //        }
    //    }
    //    protected override void OnRenderItemCheck(ToolStripItemImageRenderEventArgs e)
    //    {
    //    }
    //}
}
